#include "Common.h"


extern "C" __declspec(dllexport) int cacPtr(TestStruct * a) {
	(*a).a = 1;
	(*a).b = 2;
	return 0;
}
extern "C" __declspec(dllexport) int cacRef(TestStruct & a) {
	a.a = 3;
	a.b = 4;
	return 0;
}
extern "C" __declspec(dllexport) bool cacPtrBool(TestStruct * a) {
	(*a).a = 1;
	(*a).b = 2;
	return true;
}
extern "C" __declspec(dllexport) bool cacRefBool(TestStruct & a) {
	a.a = 3;
	a.b = 4;
	return true;
}


extern "C" __declspec(dllexport) int CalculateCheckSumShort(byte data[], int len)
{
	unsigned int result = 0;
	for (int i = 0; i < len; i++)
	{
		unsigned datum = data[i];
		result += datum;

	}
	return result;
}

extern "C" __declspec(dllexport) bool GetStruct(byte data[], int len, MyStruct1 & result) {
	result.a = new MyStruct[len];
	for (int i = 0; i < len; i++)
	{
		MyStruct cache;
		cache.a = i;
		cache.b = i;
		cache.hubId = new char[6];
		cache.hubId[0] = 't';
		cache.hubId[1] = 'e';
		cache.hubId[2] = 's';
		cache.hubId[3] = 't';
		cache.hubId[4] = (char)('1' + i);
		cache.hubId[5] = 0;
		cache.testByte = new byte[1];
		cache.testByte[0] = i;
		cache.hubIdLen = 6;
		cache.testByteLen = 1;
		result.a[i] = cache;
	}
	result.len = len;
	return true;
}
/*bool GetStruct1(byte data[], int len, MyStruct1& result) {
	result.a = new MyStruct[len];
	for (int i = 0; i < len; i++)
	{
		MyStruct cache;
		cache.a = i;
		cache.b = i;
		cache.hubId = new char[6];
		cache.hubId[0] = 't';
		cache.hubId[1] = 'e';
		cache.hubId[2] = 's';
		cache.hubId[3] = 't';
		cache.hubId[4] = (char)('1' + i);
		cache.hubId[5] = 0;
		cache.testByte = new byte[1];
		cache.testByte[0] = i;
		cache.hubIdLen = 6;
		cache.testByteLen = 1;
		result.a[i] = cache;
	}
	result.len = len;
	return true;
}

unsigned char* bin_to_strhex(const unsigned char* bin, unsigned int binsz, unsigned char** result) {
	unsigned char  hex_str[] = "0123456789abcdef";
	unsigned int      i;
	if (!(*result = (unsigned char*)malloc(binsz * 2 + 1)))
		return (NULL);
	(*result)[binsz * 2] = 0;
	if (!binsz)    return (NULL);
	for (i = 0; i < binsz; i++) {
		(*result)[i * 2 + 0] = hex_str[(bin[i] >> 4) & 0x0F];

		(*result)[i * 2 + 1] = hex_str[(bin[i]) & 0x0F];
	}  return (*result);
}

int main() {
	byte data[] = { 1,2,3,4 };
	MyStruct1 MyStruct1;
	GetStruct1(data, 4, MyStruct1);
	MyStruct* stt = MyStruct1.a;
	for (int i = 0; i < MyStruct1.len; i++) {
		MyStruct cache = stt[i];
		unsigned char** byteHex = new unsigned char* [1];
		bin_to_strhex((unsigned char*)cache.testByte, cache.testByteLen, byteHex);
		printf("cache %d [a = %d,b = %d,testByte = %s,hubId = %s]", i, cache.a, cache.b, *byteHex, cache.hubId);
	}
	return 0;
}*/